package com.ysd.resourceusestatistics.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.ysd.resourceusestatistics.model.Member;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 * POIUtil
 */
public class POIUtil {

    
// 导入会员信息
public static List<Member> importMember(String path) throws IOException {
    List<Member> list = new ArrayList<Member>();
    //File file = new File("C:\\Users\\59262\\Desktop\\会员登记表.xlsx");
    File file = new File(path);
    // 获得该文件的输入流
    FileInputStream stream = new FileInputStream(file);
    // 多态 抛异常
    Workbook sheets = new XSSFWorkbook(stream);
    // 获取一个工作表(sheet页)，下标从0开始
    Sheet sheet = sheets.getSheetAt(0);
    for (int i = 1; i <= sheet.getLastRowNum(); i++) {

        // 获取行数
        Row row = sheet.getRow(i);
        // 获取单元格 取值
        String name = row.getCell(0).getStringCellValue();
        String sex = row.getCell(1).getStringCellValue();
        String category = row.getCell(2).getStringCellValue();
        String card = row.getCell(3).getStringCellValue();
        String department = row.getCell(4).getStringCellValue();
        String profession = row.getCell(5).getStringCellValue();
        String grade = row.getCell(6).getStringCellValue();
        String phone = row.getCell(7).getStringCellValue();
        //String remark = row.getCell(8).getStringCellValue();

        
        //数据校验


        

        Member m = new Member();
        m.setCardNo(CardUtil.getCardNo());
        m.setCard(card);
        m.setCategory(category);
        m.setDepartment(department);
        m.setGrade(grade);
        m.setName(name);
        m.setPhone(phone);
        m.setProfession(profession);
        //m.setRemark(remark);
        m.setSex(sex);

        list.add(m);
    }

    // 关流
    sheets.close();
    stream.close();

    return list;
}

}